#!/usr/bin/env php
<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'app/init.php';
require_once APPPATH.'/../tests/Test.php';
require_once APPPATH.'/../tests/AirHttpTest.php';
require_once APPPATH.'/../tests/models/TestTank.php';
require_once APPPATH.'/../tests/models/TestSource.php';
require_once 'tank/Discriminator.php';

// init
AIR2_DBManager::init();
$conn = AIR2_DBManager::get_master_connection();
$browser = new AirHttpTest();
$browser->set_test_user();
$browser->set_content_type(AirHttpTest::$JSON);

$tbl_fact = Doctrine::getTable('Fact');
$tbl_fv = Doctrine::getTable('FactValue');
plan(36);

/* get some fact/fact_value ids */
$f_gender = $tbl_fact->findOneBy('fact_identifier', 'gender')->fact_id;
$fv_gender_1 = $tbl_fv->findOneBy('fv_value', 'Male')->fv_id;
$fv_gender_2 = $tbl_fv->findOneBy('fv_value', 'Female')->fv_id;

function reset_source() {
    global $s, $f_gender, $fv_gender_1;
    if ($s) $s->delete();
    $s = new TestSource();
    $s->src_first_name = 'Testfirstname';
    $s->src_last_name = 'Testlastname';
    $s->SrcFact[0]->sf_fact_id = $f_gender;
    $s->SrcFact[0]->sf_fv_id = $fv_gender_1;
    $s->save();
}
function reset_tank() {
    global $tank, $ts, $s;
    if ($tank) $tank->delete();

    $tank = new TestTank();
    $tank->tank_user_id = 1;
    $tank->tank_type = Tank::$TYPE_FB;
    $tank->tank_status = Tank::$STATUS_IMPORTED;
    $tank->TankSource[0]->src_id = $s->src_id;
    $tank->save();

    $ts = $tank->TankSource[0];
    $tank->clearRelated();
}
function reset_all() {
    reset_source();
    reset_tank();
    refresh_all();
}
function refresh_all() {
    global $tank, $ts, $s;
    $tank->clearRelated();
    $tank->refresh();
    $s->clearRelated();
    $s->refresh();
    $ts->clearRelated();
    $ts->refresh();
}


/**********************
 * Test status codes
 */
reset_all();
$nav = 'import/'.$tank->tank_uuid.'/sources/'.$ts->tsrc_id.'/resolve';
ok( $resp = $browser->http_get($nav), 'GET resolver' );
is( $browser->resp_code(), 405, 'GET resolver - resp code');
ok( $resp = $browser->http_put($nav), 'PUT resolver' );
is( $browser->resp_code(), 405, 'PUT resolver - resp code');
ok( $resp = $browser->http_delete($nav), 'DELETE resolver' );
is( $browser->resp_code(), 405, 'DELETE resolver - resp code');

$bad_nav1 = 'import/DOESNOTEXIST/sources/'.$ts->tsrc_id.'/resolve';
$bad_nav2 = 'import/'.$tank->tank_uuid.'/sources/DOESNOTEXIST/resolve';
ok( $resp = $browser->http_post($bad_nav1), 'resolver bad_nav1' );
is( $browser->resp_code(), 404, 'resolver bad_nav1 - resp code');
ok( $resp = $browser->http_post($bad_nav2), 'resolver bad_nav2' );
is( $browser->resp_code(), 404, 'resolver bad_nav2 - resp code');

// default status is STATUS_NEW ... only CONFLICTs can be resolved
ok( $resp = $browser->http_post($nav), 'resolver status' );
is( $browser->resp_code(), 412, 'resolver status - resp code');


/**********************
 * Create a conflict
 */
$tank->tank_status = Tank::$STATUS_DISCRIM_ERRORS;
$tank->save();
$ts->tsrc_status = TankSource::$STATUS_CONFLICT;
$ts->src_first_name = 'Harold';
$ts->sa_last_name = 'Aliaslast';
$ts->TankFact[0]->tf_fact_id = $f_gender;
$ts->TankFact[0]->sf_fv_id = $fv_gender_2;
$ts->save();


/**********************
 * Resolve half the conflict
 */
$params = array(
    'ops' => json_encode(array('Source' => 'R')),
);
ok( $resp = $browser->http_post($nav, $params), 'half-resolver' );
is( $browser->resp_code(), 200, 'half-resolver - resp code');
ok( $json = json_decode($resp,true), 'half-resolver - json' );
is( $json['success'], false, 'half-resolver - unsuccess' );
ok( isset($json['radix']), 'half-resolver - radix');
$errs = json_decode($json['radix']['tsrc_errors'], true);
ok( $errs, 'half-resolver - errors');
is( count($errs['resolve']), 1, 'half-resolver - conflict count');
is( $errs['resolve'][0]['model'], "Fact.$f_gender", 'half-resolver - conflict match' );


/**********************
 * Check the actual data
 */
refresh_all();
is( $tank->tank_status, Tank::$STATUS_DISCRIM_ERRORS, 'unresolved - tank_status' );
is( $ts->tsrc_status, TankSource::$STATUS_CONFLICT, 'unresolved - tsrc_status' );
isnt( $s->src_first_name, $ts->src_first_name, 'unresolved - first name not imported' );

/**********************
 * Resolve the entire conflict
 */
$params = array(
    'ops' => json_encode(array('Source' => 'R', "Fact.$f_gender" => 'I'))
);
ok( $resp = $browser->http_post($nav, $params), 'whole-resolver' );
is( $browser->resp_code(), 200, 'whole-resolver - resp code');
ok( $json = json_decode($resp,true), 'whole-resolver - json' );
is( $json['success'], true, 'whole-resolver - success' );
ok( isset($json['radix']), 'whole-resolver - radix');
ok( !$json['radix']['tsrc_errors'], 'whole-resolver - no errors');
is( $json['radix']['tsrc_status'], TankSource::$STATUS_DONE, 'whole-resolver - status');


/**********************
 * Check the actual data
 */
refresh_all();
is( $tank->tank_status, Tank::$STATUS_DISCRIMINATED, 'resolved - tank_status' );
is( $ts->tsrc_status, TankSource::$STATUS_DONE, 'resolved - tsrc_status' );
is( $s->src_first_name, $ts->src_first_name, 'resolved - first name imported' );
is( count($s->SrcAlias), 1, 'resolved - SrcAlias count' );
is( $s->SrcFact[0]->sf_fact_id, $f_gender, 'resolved - fact_id' );
is( $s->SrcFact[0]->sf_fv_id, $fv_gender_1, 'resolved - fv_id ignored' );

